<?php
namespace app\api\controller;

use think\Controller;
use think\Request;
use think\Db;

class Pingan extends Controller
{
    public function getAreas($pid = 0)
    {
//        dd($pid);
//        var_dump($pid);
//        die;
        $areas = Db::name('area')->where('pid', $pid)->select();
        return json(['code' => 0,'pid'=>$pid, 'data' => $areas]);
    }



    public function submit(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();

            // 验证数据
           $result = $this->validate($data, [
                'name' => 'require|max:255',
                'idcard' => 'require|max:255',
                'contact' => 'require|max:255',
                'province' => 'require|max:255',
                'city' => 'require|max:255',
                'district' => 'require|max:255',
                'content' => 'require',
                'file' => 'max:255',
            ], [
                'name.require' => '姓名不能为空',
                'name.max' => '姓名不能超过255个字符',
                'idcard.require' => '身份证不能为空',
                'idcard.max' => '身份证不能超过255个字符',
                'contact.require' => '联系方式不能为空',
                'contact.max' => '联系方式不能超过255个字符',
                'province.require' => '省份不能为空',
                'province.max' => '省份不能超过255个字符',
                'city.require' => '城市不能为空',
                'city.max' => '城市不能超过255个字符',
                'district.require' => '区县不能为空',
                'district.max' => '区县不能超过255个字符',
                'content.require' => '内容不能为空',
                'file.max' => '文件名不能超过255个字符',
            ]);


            if (true !== $result) {
                // 验证失败 输出错误信息
                return json(['code' => 1, 'msg' => $result]);
            }
            $data['create_time'] = date('Y-m-d H:i:s',time());
            // 存储数据
            $insertId = Db::name('list')->insertGetId($data);
            if ($insertId) {
                return json(['code' => 0, 'msg' => '提交成功']);
            } else {
                return json(['code' => 1, 'msg' => '提交失败']);
            }
        }

}

}